package app.auto.runner.base;

import android.content.Intent;
import android.text.TextUtils;

//import com.wheel.WheelPicker;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import app.auto.AndroidInstance;
import app.auto.runner.base.intf.FunCallback;
import app.auto.runner.base.intf.MapBuilder;
import app.auto.runner.base.intf.json.JsonToMapUtils;
import app.auto.runner.base.widget.DatePicker;
import app.auto.runner.base.widget.TextPicker;

public class Constant extends AndroidInstance {
    public final static String control_image = "<image/>";
    public final static String control_txt = "<txt/>";
    public final static String control_date = "<date/>";
    public final static String control_number = "<number/>";
    public final static String control_time = "<time/>";

    public static Map control_map;
    public static Map control_text_starttype_map = new HashMap();

    {
        control_text_starttype_map.put("broadcast", BroadcastBuilder.Starttype.broadcast);

    }

    public static Map control_text_font_map = new HashMap();

    {
        control_text_font_map.put("numbercallback", new FunCallback() {
            @Override
            public void onCallback(Object result, Object object) {
                super.onCallback(result, object);
                String m = ((Intent) object).getStringExtra("value");
                String t = ((Intent) object).getStringExtra("title");
                String max = ((Intent) object).getStringExtra("max");
                int maxI = TextUtils.isEmpty(max) ? 1000 : Integer.parseInt(max);

                String min = ((Intent) object).getStringExtra("min");

                int minI = TextUtils.isEmpty(max) ? 0 : Integer.parseInt(min);


//                WheelPicker.getRange(getContext(), t, minI, maxI, 5, new WheelPicker.FuncCallback() {
//                    @Override
//                    public void callback(String... date) {
//                        String rt = date[1];
//                        BroadcastBuilder.build().sendAction("<number/>back", getContext(), MapBuilder.build().add("rt", rt).add("isok", true), BroadcastBuilder.Starttype.broadcast);
//                    }
//                });
            }
        });
        control_text_font_map.put("timecallback", new FunCallback() {
            @Override
            public void onCallback(Object result, Object object) {
                super.onCallback(result, object);
                String m = ((Intent) object).getStringExtra("value");
                String t = ((Intent) object).getStringExtra("title");

//                WheelPicker.getDate(getContext(), t, new Date(), new WheelPicker.FuncCallback() {
//                    @Override
//                    public void callback(String... date) {
//                        String rt = date[1] + "/" + date[2] + " " + date[3] + ":" + date[4];
//                        BroadcastBuilder.build().sendAction("<date/>back", getContext(), MapBuilder.build().add("rt", rt), BroadcastBuilder.Starttype.broadcast);
//                    }
//                });
            }
        });
        control_text_font_map.put("txtcallback", new FunCallback() {
            @Override
            public void onCallback(Object result, Object object) {
                super.onCallback(result, object);
                String m = ((Intent) object).getStringExtra("value");
                String t = ((Intent) object).getStringExtra("title");
                String digits = ((Intent) object).getStringExtra("digits");

                TextPicker.get(TextPicker.class, getContext()).setTxt(m).setDigits(digits).pick(new FunCallback() {
                                                                                                    @Override
                                                                                                    public void onCallback(Object result, Object object) {
                                                                                                        super.onCallback(result, object);
                                                                                                        String rt = (String) result;
                                                                                                        BroadcastBuilder.build().sendAction("<txt/>back", getContext(), MapBuilder.build().add("rt", rt).add("isok", true), BroadcastBuilder.Starttype.broadcast);
                                                                                                    }
                                                                                                }
                        , new FunCallback() {
                            @Override
                            public void onCallback(Object result, Object object) {
                                super.onCallback(result, object);
                                BroadcastBuilder.build().sendAction("<txt/>back", getContext(), MapBuilder.build(), BroadcastBuilder.Starttype.broadcast);

                            }
                        });
            }
        });
        control_text_font_map.put("imagecallback", new FunCallback() {
            @Override
            public void onCallback(Object result, Object object) {
                super.onCallback(result, object);
                String t = ((Intent) object).getStringExtra("title");

//                WheelPicker.getRange(getContext(), t, 0, 1000, 5, new WheelPicker.FuncCallback() {
//                    @Override
//                    public void callback(String... date) {
//                        String rt = date[1];
//                        BroadcastBuilder.build().sendAction("<number/>back", getContext(), MapBuilder.build().add("rt", rt), BroadcastBuilder.Starttype.broadcast);
//                    }
//                });
            }
        });
        control_text_font_map.put("datecallback", new FunCallback() {
            @Override
            public void onCallback(Object result, Object object) {
                super.onCallback(result, object);
                String m = ((Intent) object).getStringExtra("value");
                String t = ((Intent) object).getStringExtra("title");

                DatePicker.get(DatePicker.class, getContext()).pick(new FunCallback() {
                    @Override
                    public void onCallback(Object result, Object object) {
                        super.onCallback(result, object);
                        Date date = ((Date) result);
                        String rt = new SimpleDateFormat("yyyy-MM-dd").format(date);
                        BroadcastBuilder.build().sendAction("<date/>back", getContext(), MapBuilder.build().add("rt", rt), BroadcastBuilder.Starttype.broadcast);
                    }
                });
            }
        });

    }


    public void init() {
        control_map = new HashMap();
        MapBuilder mapBuilder = MapBuilder.build().add("location", "<image/>").add("type", "broadcast").add("front", "imagecallback");
        String json = JsonToMapUtils.mapToJson(mapBuilder.get());
        control_map.put(control_image, json);

        mapBuilder = MapBuilder.build().add("location", "<txt/>").add("type", "broadcast").add("front", "txtcallback");
        json = JsonToMapUtils.mapToJson(mapBuilder.get());
        control_map.put(control_txt, json);

        mapBuilder = MapBuilder.build().add("location", "<date/>").add("type", "broadcast").add("front", "datecallback");
        json = JsonToMapUtils.mapToJson(mapBuilder.get());
        control_map.put(control_date, json);

        mapBuilder = MapBuilder.build().add("location", "<number/>").add("type", "broadcast").add("front", "numbercallback");
        json = JsonToMapUtils.mapToJson(mapBuilder.get());
        control_map.put(control_number, json);

        mapBuilder = MapBuilder.build().add("location", "<time/>").add("type", "broadcast").add("front", "timecallback");
        json = JsonToMapUtils.mapToJson(mapBuilder.get());
        control_map.put(control_time, json);


    }


    public static Map getControl_text_starttype_map() {
        return control_text_starttype_map;
    }

    public static Map getControl_text_font_map() {
        return control_text_font_map;
    }

    FunCallback imagecallback = new FunCallback() {
        @Override
        public void onCallback(Object result, Object object) {
            super.onCallback(result, object);
        }
    };

    FunCallback txtcallback = new FunCallback() {
        @Override
        public void onCallback(Object result, Object object) {
            super.onCallback(result, object);
        }
    };

    FunCallback datecallback = new FunCallback() {
        @Override
        public void onCallback(Object result, Object object) {
            super.onCallback(result, object);
        }
    };


    FunCallback timecallback = new FunCallback() {
        @Override
        public void onCallback(Object result, Object object) {
            super.onCallback(result, object);

        }
    };


    public String get(String key) {
        if (control_map == null) {
            init();
        }
        return (String) control_map.get(key);
    }

    @Override
    public AndroidInstance use(Object mp, Object... p) {
        return null;
    }


}
